package exsun.jt808.data.media.operate.encode.codec;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;

public class Easy_AACEncoder {
    static {
        //                    System.load("C:\\Users\\Admin\\Desktop\\java-flv-video\\libEasyAACEncoder.so");
        System.loadLibrary("libEasyAACEncoder");
//        System.load("/data/libEasyAACEncoder.so");
    }
    //audioSamplerate 采样率   audioChannel 声道数  PCMBitSize 单样本位数
    // AudioCodec:  Law_uLaw  Law_ALaw Law_PCM16 Law_G726
    //    Law_ULaw	=	0, 		/**< U law */
    //    Law_ALaw	=	1, 		/**< A law */
    //    Law_PCM16	=	2, 		/**< 16 bit uniform PCM values. 原始 pcm 数据 */
    //    Law_G726	=	3		/**< G726 */
    public static native long Easy_AACEncoder_Init(int audioSamplerate,byte audioChannel,int PCMBitSize,byte AudioCodec,int rateBits);
    public static native byte[] Easy_AACEncoder_Encode(long handle,byte[] in);
    public static native long Easy_AACEncoder_Release(long handle);

    public static void loadLib(String libName) throws IOException {
        String systemType = System.getProperty("os.name");
        String libExtension = (systemType.toLowerCase().indexOf("win")!=-1) ? ".dll" : ".so";
        String libFullName = libName + libExtension;
        String nativeTempDir = System.getProperty("java.io.tmpdir");
        InputStream in = null;
        BufferedInputStream reader = null;
        FileOutputStream writer = null;
        File extractedLibFile = new File(nativeTempDir+ File.separator+libFullName);
        if(!extractedLibFile.exists()){
            try {
                in = Easy_AACEncoder.class.getResourceAsStream("C:\\Users\\Admin\\Desktop\\java-flv-video" + libFullName);
                if(in==null)
                    in = Easy_AACEncoder.class.getResourceAsStream(libFullName);
                Easy_AACEncoder.class.getResource(libFullName);
                reader = new BufferedInputStream(in);
                writer = new FileOutputStream(extractedLibFile);
                byte[] buffer = new byte[1024];
                while (reader.read(buffer) > 0){
                    writer.write(buffer);
                    buffer = new byte[1024];
                }
            } catch (IOException e){
                e.printStackTrace();
            } finally {
                if(in!=null)
                    in.close();
                if(writer!=null)
                    writer.close();
            }
        }
        System.load(extractedLibFile.toString());
    }


}
